package com.xjnt.mnui.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.xjnt.frameworks.annotation.Service;
import com.xjnt.frameworks.core.BaseService;
import com.xjnt.mnui.entity.SubjectCount;

/**
 * 类说明：专题统计管理service类
 * 
 * @author 作者 LUXIN
 * @version 创建时间：2015年12月2日11:22:36
 */
@Service
public class SubjectCountService extends BaseService<SubjectCount> {

	@Override
	public SubjectCount getModel() {
		return SubjectCount.me;
	}
	/**
	 * 获得专题访问次数统计list
	 * @param map
	 * @return list
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<Map> getCountBySubject(Map<String, Object> map) {
		int pageNumber = Integer.valueOf(map.get("pageNumber").toString());//页号
		int pageSize = Integer.valueOf(map.get("pageSize").toString());//每页显示个数
		//查询条件
		Object queryType = map.get("queryType");
		Object subName = map.get("subName");
		Object dataBegin = map.get("dataBegin");
		Object dataEnd = map.get("dataEnd");
		
		String con = "";//查询条件sql
		String selectPart = "c.sub_create_time as beginTime, '"+getNowTime()+"' as endTime ";//init时查询条件
		if(subName != null){
			if(Integer.valueOf(queryType.toString()) == 1){//精确
				con += "and c.sub_name = '"+subName+"'";
			}else{//模糊
				con += "and c.sub_name like '%"+subName+"%'";
			}
		}
		if(dataBegin != null && dataEnd == null){//大于等于起始时间 小于等于当前时间
			con += " AND c.create_time BETWEEN '"+dataBegin+" 00:00:00' AND '"+getNowTime()+" 23:59:59' ";
			selectPart = "'"+dataBegin+" 00:00:00' as beginTime, '"+getNowTime()+"' as endTime ";
		}
		else if(dataEnd != null && dataBegin == null){//小于等于结束时间 大于等于专题上线时间
			con += " AND c.create_time BETWEEN c.sub_create_time AND '"+dataEnd+" 23:59:59' ";
			selectPart = "c.sub_create_time as beginTime, '"+dataEnd+" 23:59:59' as endTime ";
		}
		else if(dataEnd != null && dataBegin != null){//在起始时间与结束时间之间
			con += " AND c.create_time BETWEEN '"+dataBegin+" 00:00:00' AND '"+dataEnd+" 23:59:59' ";
			selectPart = "'"+dataBegin+" 00:00:00' as beginTime, '"+dataEnd+" 23:59:59' as endTime ";
		}
		//执行查询
		SubjectCount subjectCount = new SubjectCount();
		
		String select = "SELECT count(*) as count, c.sub_code, c.sub_name,c.sub_create_time,"+selectPart;
		String sqlExceptSelect = "FROM mnui_subject_count c where 1=1 "+con+" GROUP BY c.sub_code";
		
		//System.out.println(select + " "+sqlExceptSelect);//完整sql
		Page<SubjectCount> page = subjectCount.paginate(pageNumber, pageSize, select, sqlExceptSelect);
		List<Record> list = Db.find(select +" "+sqlExceptSelect);
		
		Map<String, Page<SubjectCount>> m1 = new HashMap<String, Page<SubjectCount>>();
		Map<String, Integer> m2 = new HashMap();
		
		m1.put("page", page);
		m2.put("total", list.size());
		
		List<Map> l = new ArrayList<Map>();
		l.add(m1);
		l.add(m2);
		
		return l;
	}
	/**
	 * 根据时间段对专题统计进行删除
	 * @param dateBegin
	 * @param dateEnd
	 */
	public void delsByDate(String dateBegin,String dateEnd){
		String sql = "delete from mnui_subject_count where create_time BETWEEN ? AND ?";
		Object[] paras = new Object[2];
		paras[0] = dateBegin+" 00:00:00";
		paras[1] = dateEnd+" 23:59:59";
		Db.update(sql, paras);
	}
	/**
	 * 获得当前时间 yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public String getNowTime(){
		Date nowTime = new Date(System.currentTimeMillis());
		SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdFormatter.format(nowTime);
	}
}
